package com.app.shop.demo.fragments;


import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.BlockedNumberContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.app.shop.demo.MyApplication;
import com.app.shop.demo.R;
import com.app.shop.demo.activitys.OrderDetailActivity;
import com.app.shop.demo.adapters.ShopCarListAdapter;
import com.app.shop.demo.beans.GoodsBean;
import com.app.shop.demo.beans.OrderBean;
import com.app.shop.mylibrary.base.BaseFragment;
import com.app.shop.mylibrary.beans.EventMessage;
import com.app.shop.mylibrary.utils.DialogUtil;
import com.app.shop.mylibrary.utils.TimeUtil;
import com.app.shop.mylibrary.utils.ToastUtil;
import com.app.shop.mylibrary.utils.UserManager;
import com.app.shop.mylibrary.widgts.CustomDialog;
import com.app.shop.mylibrary.widgts.ScrollListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * A simple {@link Fragment} subclass.
 */
public class   ShopCarFragment extends BaseFragment {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv)
    ScrollListView lv;
    @BindView(R.id.tv_total_price)
    TextView tvTotalPrice;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.rela_empty)
    RelativeLayout relaEmpty;

    ShopCarListAdapter adapter;
    private List<GoodsBean> list = new ArrayList<>();
    private double price_total;

    private String dialog_title = "提交订单";
    private String dialog_content = "是否确定提交订单？";
    private CustomDialog customDialog;

    public ShopCarFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop_car, container, false);
        ButterKnife.bind(this, view);
        registerEventBus();

        initView();
        return view;
    }


    private void initView() {
        list.clear();
        list.addAll(MyApplication.goodsBeanList);
        adapter = new ShopCarListAdapter(getContext(), (ArrayList) list, R.layout.item_shop_car);
        lv.setAdapter(adapter);
        setPrice(list);

        if (list.size() > 0) {
            lv.setVisibility(View.VISIBLE);
            relaEmpty.setVisibility(View.GONE);
        } else {
            lv.setVisibility(View.GONE);
            relaEmpty.setVisibility(View.VISIBLE);
        }
    }

    private void setPrice(List<GoodsBean> beanList) {
        price_total = 0;
        for (int i = 0; i < beanList.size(); i++) {
            price_total += (beanList.get(i).getGoods_price() * beanList.get(i).getGoods_num());
        }

        tvTotalPrice.setText("￥" + price_total);
    }

    @Override
    public void onEvent(EventMessage msg) {
        super.onEvent(msg);
        if (msg.getMessageType() == EventMessage.REFRESH) {
            list.clear();
            list.addAll(MyApplication.goodsBeanList);
            adapter.notifyDataSetChanged();
            setPrice(MyApplication.goodsBeanList);

            if (list.size() > 0) {
                lv.setVisibility(View.VISIBLE);
                relaEmpty.setVisibility(View.GONE);
            } else {
                lv.setVisibility(View.GONE);
                relaEmpty.setVisibility(View.VISIBLE);
            }
        }
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {

        if (!UserManager.isLogin(getContext())) {
            ToastUtil.showToast(getContext(), "请先登录");
            return;
        }

        if (MyApplication.goodsBeanList.size() == 0) {
            ToastUtil.showToast(getContext(), "您还没有添加商品");
            return;
        }
        customDialog = DialogUtil.showDialog(getActivity(), customDialog, 2, dialog_title, dialog_content, "取消", "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

                MyApplication.orderDetailList.clear();
                MyApplication.orderDetailList.addAll(MyApplication.goodsBeanList);

                OrderBean orderBean = new OrderBean();
                orderBean.setOrder_time(TimeUtil.getTodayData("yyyy-MM-dd HH-mm-ss"));
                orderBean.setGoodsBeanList(MyApplication.orderDetailList);

                //进入订单详情
                Bundle bundle = new Bundle();
                bundle.putSerializable("orderBean", orderBean);

                skipActivity(getActivity(), OrderDetailActivity.class, bundle);

                MyApplication.orderList.add(orderBean);
                MyApplication.goodsBeanList.clear();
                list.clear();
                adapter.notifyDataSetChanged();
                EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
            }
        });

        if (customDialog != null && !customDialog.isShowing()) {
            customDialog.show();
        }


        customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                customDialog = null;
            }
        });
    }
}
